<?php


namespace app\admin\freight\logic;


use app\admin\system\logic\AreaLogic;
use app\common\logic\BaseLogic;
use app\model\freight\Freight;
use app\model\freight\FreightRule;
use stdClass;
use support\utils\ArrayUtil;
use support\utils\Time;

class FreightLogic extends BaseLogic
{
    public function __construct()
    {
        $this->model = new Freight();
    }

    public function addFreight($data)
    {
        $rules = $data['rules'];
        unset($data['rules']);
        $data["create_time"] = Time::now();
        $data["update_time"] = Time::now();
        $freight_id = $this->model->insertGetId($data);
        $insertRule = [];
        foreach ($rules as $datum) {
            $insertRule[] = [
                "freight_id" => $freight_id,
                "text" => $datum['text'] ?? "",
                "cityList" => implode(',', $datum['cityList'] ?? []),
                "provinceList" => implode(",", $datum['provinceList'] ?? []),
                "first" => $datum['first'] ?? 0,
                "first_fee" => $datum['first_fee'] ?? 0,
                "additional" => $datum['additional'] ?? 0,
                "additional_fee" => $datum['additional_fee'] ?? 0,
                "create_time" => Time::now(),
            ];
        }
        FreightRule::insertAll($insertRule);

    }

    public function editFreight($data)
    {
        $one = $this->model->find($data['id']);
        $one->save($data);
        $insertRule = [];
        FreightRule::where([
            "freight_id" => $one['id'],
        ])->delete();
        foreach ($data['rules'] as $datum) {
            $insertRule[] = [
                "freight_id" => $one['id'],
                "text" => $datum['text'] ?? "",
                "cityList" => implode(',', $datum['cityList'] ?? []),
                "provinceList" => implode(",", $datum['provinceList'] ?? []),
                "first" => $datum['first'] ?? 0,
                "first_fee" => $datum['first_fee'] ?? 0,
                "additional" => $datum['additional'] ?? 0,
                "additional_fee" => $datum['additional_fee'] ?? 0,
                "create_time" => Time::now(),
            ];
        }
        FreightRule::insertAll($insertRule);

    }

    public function readFreight($id)
    {
        $data = Freight::where("id", $id)
            ->with(['rules'])
            ->find()
            ->toArray();

        if ($data['rules']) {
            $logic = new AreaLogic;
            foreach ($data['rules'] as $k => &$value) {
                $provinceList = $logic->where("code", 'in', $value['provinceList'])->field("name,code")->select()->toArray();
                $cityList = $logic->where("parent_code", 'in', $value['provinceList'])->field("name,code,parent_code")->select()->toArray();
                $allCheck = $logic->where("parent_code", 'in', $value['provinceList'])->field("parent_code,count(1) as count")->group('parent_code')->select()->toArray();
                $cityList = ArrayUtil::group($cityList, 'parent_code');
                $allCheck = array_column($allCheck, 'count', 'parent_code');
                foreach ($provinceList as $key => $item) {
                    $provinceList[$key]['cityList'] = $cityList[$item['code']] ?? [];
                    $provinceList[$key]['isAll'] = ($allCheck[$item['code']] ?? 0) == count($provinceList[$key]['cityList']);
                }
                $value["selectedData"] = $provinceList ? array_column($provinceList, null, 'code') : new stdClass();
            }
        }
        return $data;

    }
}